//
//  YAYIPatientComeController.m
//  YAYIDoctor
//
//  Created by hxp on 16/4/29.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIPatientComeController.h"
#import "YAYINavigationController.h"
#import "YAYIPatienInfoController.h"
#import "YAYIPatientComeCell.h"
#import "YAYIPatientModel.h"


@interface YAYIPatientComeController ()<UITableViewDelegate,UITableViewDataSource, YAYIPatienInfoDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArry;


// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@end

@implementation YAYIPatientComeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArry = [NSMutableArray array];
    self.title = @"新的患者";
    
    [self creatTableView];
    [self createNetRequest];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}



-(void)creatTableView
{
    if (self.tableView ==  nil) {
        self.tableView = [UITableView new];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableFooterView = [UIView new];
        self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:self.tableView];
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, SCREEN_height));
    }];

}


- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"没有新的患者";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-50);
    }];
}


#pragma mark - 获取数据
-(void)createNetRequest
{
    [self.dataArry removeAllObjects];
    [YAYINetWorking getWithURL:newBdPatient_url param:@{@"reportstate": @"0"} finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回：%@", dict);
        
        if ([dict[@"status"] isEqualToString:@"success"]) {
            NSArray *data = dict[@"data"];
            for (NSDictionary *obj in data) {
                YAYIPatientModel *model = [YAYIPatientModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataArry  addObject:model];
            }
            [self.tableView reloadData];
        }
        
        if (0 == self.dataArry.count) {
            [self showNoDataFlag:YES];
            
        }else{
            [self showNoDataFlag:NO];
        }
    }];
    
}


#pragma mark - 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"identifer";
    YAYIPatientComeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIPatientComeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArry[indexPath.row];
    cell.button.tag = indexPath.row;
    if ([cell.model.reportstate intValue] == 0) {
        [cell.button setTitle:@"接受" forState:UIControlStateNormal];
        [cell.button  addTarget:self action:@selector(acceptPatient:) forControlEvents:UIControlEventTouchUpInside];
         [cell.button setBackgroundColor: YAYIColor(83, 165, 243)];

    }else if ([cell.model.reportstate intValue] == 1){
        [cell.button setTitle:@"已接受" forState:UIControlStateNormal];
        [cell.button setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
    }else{
        [cell.button setTitle:@"已拒绝" forState:UIControlStateNormal];
        [cell.button setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
    }
        return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 84;
}


-(void)acceptPatient:(UIButton *)sender
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = [self.dataArry[sender.tag] id];
    param[@"reportstate"] = @"1";
    
    YAYILog(@"param:%@", param);
    
    [YAYINetWorking  postJsonSerilizeWithURL:acceptBdPatient_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error,NSDictionary *responseError) {
        
        if (error == nil) {
            if ([dict[@"status"] isEqualToString:@"success"]) {
                
                [sender setTitle:@"已接受" forState:UIControlStateNormal];
                sender.userInteractionEnabled = NO;
                [sender setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
                
                if ([self.delegate respondsToSelector:@selector(patientComeControllerAccepted)]) {
                    [self.delegate patientComeControllerAccepted];
                }
            }

        }else{
            NSString *message = responseError[@"message"];
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YAYIPatientComeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    BOOL isAccepted = !cell.button.userInteractionEnabled;
    
    YAYIPatienInfoController *patienInfo = [YAYIPatienInfoController new];
    patienInfo.isAccepted = isAccepted;
    patienInfo.delegate = self;
    patienInfo.userid = [self.dataArry[indexPath.row] userid];
    patienInfo.id = [self.dataArry[indexPath.row] id];
    [self.navigationController pushViewController:patienInfo animated:YES];

}


#pragma mark YAYIPatienInfoController代理
- (void)patienInfoAcceptOrDefine
{
    if ([self.delegate respondsToSelector:@selector(patientComeControllerAccepted)]) {
        [self.delegate patientComeControllerAccepted];
    }
    
    [self createNetRequest];
    
}


@end
